﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Shapes;

namespace Hugo
{
    public class Util
    {

        [System.Runtime.InteropServices.DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);

        public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
        {
            IntPtr ip = source.GetHbitmap();
            BitmapSource bs = null;
            try
            {
                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
                   IntPtr.Zero, Int32Rect.Empty,
                   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(ip);
            }

            return bs;
        }

        public static Rectangle getSymbol(BitmapSource u, int height, int width)
        {
            Rectangle item = new Rectangle();
            item.Height = height;
            item.Width = width;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            myBrush.ImageSource = u;
            item.Fill = myBrush;

            return item;
        }

        public static int getFontHeightFromSize(int fontSize)
        {
            System.Drawing.FontFamily ff = new System.Drawing.FontFamily("Arial");
            int ascentPixel = 
                fontSize * ff.GetCellAscent(System.Drawing.FontStyle.Regular) / ff.GetEmHeight(System.Drawing.FontStyle.Regular);
            return ascentPixel;
        }
    }
}
